package com.locationtracker.plus.ui.fragment.memeber

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.kingja.loadsir.core.LoadService
import com.locationtracker.plus.R
import com.locationtracker.plus.app.base.BaseFragment
import com.locationtracker.plus.app.eventViewModel
import com.locationtracker.plus.app.ext.*
import com.locationtracker.plus.app.weight.recyclerview.decoration.VerticalSpacingItemDecoration
import com.locationtracker.plus.databinding.FragmentMemberBinding
import com.locationtracker.plus.ui.adapter.MemberAdapter
import com.locationtracker.plus.ui.dialog.PhoneInputDialog
import com.locationtracker.plus.viewmodel.request.RequestMemberViewModel
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.dp2px

class MemberFragment : BaseFragment<BaseViewModel, FragmentMemberBinding>() {

    //适配器
    private val memberAdapter by lazy { MemberAdapter(arrayListOf()) }

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    private var phoneDialog: PhoneInputDialog? = null

    private val requestMemberViewModel by viewModels<RequestMemberViewModel>()

    private val firstOrRetryRequest: () -> Unit = {
        loadsir.showLoading()
        requestMemberViewModel.getMemberData(true)
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeHeader.headerBack.setOnClickListener { nav().navigateUp() }
        mDatabind.includeHeader.headerTitle.text = getString(R.string.member)
        //状态页配置
        loadsir = loadServiceInit(mDatabind.includeRecyclerview.swipeRefresh, firstOrRetryRequest)
        mDatabind.floatBtn.setOnClickListener {
            nav().jumpBySubscribe {
                PhoneInputDialog(mActivity).let {
                    phoneDialog = it
                    it.show()
                    it.window?.decorView?.background = null
                }
            }
        }
        //初始化recyclerView
        mDatabind.includeRecyclerview.recyclerView.init(
            LinearLayoutManager(mActivity),
            memberAdapter
        ).let {
            if (it.itemDecorationCount == 0)
                it.addItemDecoration(
                    VerticalSpacingItemDecoration(
                        mActivity.dp2px(16),
                        mActivity.dp2px(12)
                    )
                )
            it.initFooter { requestMemberViewModel.getMemberData(false) }
        }
        //初始化 SwipeRefreshLayout
        mDatabind.includeRecyclerview.swipeRefresh.init {
            //触发刷新监听时请求数据
            requestMemberViewModel.getMemberData(true)
        }
        memberAdapter.run {
            setOnItemClickListener { _, _, position ->
                if (data[position].phone == "Me") {
                    nav().navigateAction(R.id.action_to_locationFragment)
                } else {
                    nav().navigateAction(
                        R.id.action_to_locationFragment,
                        bundleOf("friendId" to data[position].id)
                    )
                }
            }
        }
    }

    override fun lazyLoadData() {
        firstOrRetryRequest.invoke()
    }

    override fun createObserver() {
        requestMemberViewModel.run {
            memberDataState.observe(viewLifecycleOwner) {
                loadListData(
                    it,
                    memberAdapter,
                    loadsir,
                    mDatabind.includeRecyclerview.recyclerView,
                    mDatabind.includeRecyclerview.swipeRefresh
                )
            }
        }
        eventViewModel.chooseContactResult.observeInFragment(this) {
            phoneDialog?.let { dialog ->
                if (dialog.isShowing) {
                    dialog.updatePhone(it)
                } else {
                    phoneDialog = null
                }
            }
        }
    }
}